// pages/indent/index.js
var app = getApp();
const http = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    texts: "",
    animation: null,
    timer: null,
    datashow:true,
    duration: 0,
    userId: 0,
    textWidth: 0,
    wrapWidth: 0,
    leve: 0,
    gooodlist: [{ 
        attr: "1",
        coverPic: "https://zhongkexiangjiu.cn//zkxj/basic/down/image?imageKey=7863ecda-777a-4494-bf75-37787d10f542.png",
        createTime: "2024-01-05 16:15:10",
        goodsNum: 999,
        id: 36,
        isDelete: 0,
        isHot: "hot",
        isLevel: 0,
        memberDiscount: 0,
        name: "烟灰缸",
        originalPrice: 66,
        price: 56,
        putawayStatus: 1,
        shelfLife: "36个月",
        sort: 1000,
        status: 1,
        typeId: 7,
        unit: "1",
        virtualSales: 0,
      }
       
      ],
    imglist: ['https://zhongkexiangjiu.cn/zkxj/basic/down/image?imageKey=f64e9a28-abcd-4011-9df9-24cf2ea766c9.jpg', 'https://zhongkexiangjiu.cn/zkxj/basic/down/image?imageKey=4bda40be-0e00-41c5-8812-fd6bf1618ef4.jpg']
  },
  onLoad(options) {
    wx.request({
      url: app.globalData.url + '/zkxj/topic/list',
      data: {

      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        //截取数组前wu个元素
const sliceA=res.data.data.slice(0,5)
        var arr = ''
        for (let i = 0; i <= sliceA.length; i++) {
          if (sliceA[i]) {
            arr += sliceA[i].title + "     ";
          }
        }
        this.setData({
          texts: arr
        })
      },
    })
    wx.request({
      url: app.globalData.url + '/zkxj/topic/photograph/queryImageList',
      data: {

      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        var arrsiumg = []
        for (let i = 0; i <= res.data.data.length; i++) {
          if (res.data.data[i]) {
            arrsiumg.push(res.data.data[i].imageUrl);
          }
        }
        console.log(arrsiumg);
        this.setData({
          imglist: arrsiumg
        })
      },
    })
    //请求商品列表
    wx.request({
      url: app.globalData.url + '/zkxj/good/hotgoods',
      data: {},
      method: 'get',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        if(res.data.code ===0){ 
            this.setData({
                gooodlist: res.data.data,
              });
        }
       
      },
    
    })
    this.data.userId = wx.getStorageSync('userId')
    console.log(this.data.userId);
    //获取真实数据渲染信息
    wx.request({
      url: app.globalData.url + '/zkxj/user/detail',
      data: {
        "userId": this.data.userId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        wx.setStorageSync('loginuser', res.data.data);
        console.log(res);
        if (res.data.code == 0) {
          this.setData({
            leve: res.data.data.level
          })
          this.setData({

            usersunm: {
              avimg: res.data.data.avatarUrl,
              userdata: res.data.data,
              useid: res.data.data.createUserId,
              addli: res.data.data.area,
            }
          })
        } else if (res.data.code == 401) {
          wx.navigateTo({
            url: '/pages/login/index',
          })
        }
        //判断是否展开商学院
        if (this.data.usersunm.userdata.level < 5) {
          this.setData({
            shangis: true
          })
        }
      }
    })

  },
  ondown() {
    console.log(1);
    this.setData({
      datashow:false
    })
    console.log(this.data.datashow);
  },
  openig() {
    console.log(this.data.leve);
    if(this.data.leve>=2){
      wx.navigateTo({
        url: '/pages/repurchase/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/retail/index',
      })
    }
  },
  handledetodelait(e) {
    const statet = e.currentTarget.dataset['state'] || 0
    console.log(statet);
    wx.navigateTo({
      url: `/pages/gooddelait/index?id=${statet}`,
    })
  },
  handletogoodli() {
    wx.navigateTo({
      url: '/pages/goodlist/index',
    })
  },
  getbanner() {
    wx.request({
      url: app.globalData.url + '/zkxj/topic/photograph/queryImageList',
      data: {

      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        var arr = ''
        for (let i = 0; i <= res.data.data.length; i++) {
          if (res.data.data[i]) {
            arr += res.data.data[i].title + "    ";
          }
        }
        console.log(arr);
        this.setData({
          text: arr
        })
      },
    })
  },
  handletap(event) {
    let {
      index
    } = event.currentTarget.dataset
    this.setData({
      idx: index
    });
  },
  handletomackup() {
    wx.navigateTo({
      url: '/pages/mackup/index',
    })
  },
  handletoinformation() {
    wx.navigateTo({
      url: '/pages/infopage/index',
    })
  },
  onShow() {
    this.initAnimation(this.data.text)
  },
  onHide() {
    this.destroyTimer()
    this.setData({
      timer: null
    })
  },
  onUnload() {
    this.destroyTimer()
    this.setData({
      timer: null
    })
  },
  destroyTimer() {
    if (this.data.timer) {
      clearTimeout(this.data.timer);
    }
  },
  /**
   * 开启公告字幕滚动动画
   * @param {String} text 公告内容
   * @return {[type]} 
   */
  initAnimation(text) {
    let that = this
    this.data.duration = 30000
    this.data.animation = wx.createAnimation({
      duration: this.data.duration,
      timingFunction: 'linear'
    })
    let query = wx.createSelectorQuery()
    query.select('.content-box').boundingClientRect()
    query.select('#text').boundingClientRect()
    query.exec((rect) => {
      that.setData({
        wrapWidth: rect[0].width,
        textWidth: rect[1].width
      }, () => {
        this.startAnimation()
      })
    })
  },
  // 定时器动画
  startAnimation() {
    //reset
    // this.data.animation.option.transition.duration = 0
    const resetAnimation = this.data.animation.translateX(this.data.wrapWidth).step({
      duration: 0
    })
    this.setData({
      animationData: resetAnimation.export()
    })
    // this.data.animation.option.transition.duration = this.data.duration
    const animationData = this.data.animation.translateX(-this.data.textWidth).step({
      duration: this.data.duration
    })
    setTimeout(() => {
      this.setData({
        animationData: animationData.export()
      })
    }, 1000)
    const timer = setTimeout(() => {
      this.startAnimation()
    }, this.data.duration)
    this.setData({
      timer
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})